/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.gui;

import it.polimi.meteocal.entities.Event;
import it.polimi.meteocal.entities.Invitation;
import it.polimi.meteocal.entities.User;
import it.polimi.meteocal.sessionbeans.UserManager;
import java.io.Serializable;  
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

@ManagedBean(name = "InviteUserBean")
// SCOPE?
@SessionScoped
public class InviteUserBean implements Serializable {
     
    @EJB
    UserManager um;
    
    private List<User> selectedUsers = new ArrayList<>();   
    private List<User> allUsers;
    
    public InviteUserBean(){
    }
    
    public List<User> all(){
        return um.readAllUsersFromDB();
    } 
    
    public List<User> getSelectedUsers(){
        System.out.println("selected get: " + selectedUsers);
        return selectedUsers; 
    }
    
    public void setSelectedUsers(List<User> selectedUsers){
        this.selectedUsers = selectedUsers;
        System.out.println("selected set: " + selectedUsers);
    }
    
    public List<User> completeUsers(String query){
        List<User> suggestions = new ArrayList<>();
        allUsers = um.readAllUsersFromDB();
        Integer organizerID = um.getLoggedUser().getIdu();
      
        System.out.println("allUsers: ");
        for(User u : this.allUsers){
            System.out.print(u.getUsername() + " " + u.getIdu());
            if(!u.getIdu().equals(organizerID) && !selectedUsers.contains(u) &&
                    u.getUsername().startsWith(query)){
                    suggestions.add(u);
            }
        }
        System.out.println("suggestions: " + suggestions);
        return suggestions; 
    }

}
